/**
 * Created by ㅋㅋElsa on 2017/8/9.
 */
layui.define(['jquery', 'layer','paging', 'code','form'], function(exports){
    var form = layui.form();

    layui.code();
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer;
    paging.init({
        url: contextPath + "/user/cc/all", //地址
        elem: '#con', //内容容器
        type: 'GET',
        //new
        key: '#s_type',
        value: '#s_value',
        params: { //发送到服务端的参数
        },
        tempElem: '#conTemp', //模块容器
        pageConfig: { //分页参数配置
            elem: '#page', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
        },
        fail: function(msg) { //获取数据失败的回调
        },
        complate: function() { //完成的回调

        },
    });

    $("body").on("click",".forbidd",function(){
        var userid = $(this).data('id');

        layer.alert('确定禁用该用户?',{
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,btn: ['确定','取消'] //按钮
            ,icon: 6    // icon
        },function(){
            $.ajax({
                type:"PUT",
                url:contextPath+"/user/forbid/" + userid,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "禁用成功!"){
                        layer.msg('禁用成功!<br>页面将在1秒后刷新...', {
                            time: 1000 //1s后自动关闭
                        });
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                    }
                },
                error:function (e) {
                }
            });
        });
    });

    $("body").on("click",".reset",function(){
        var userid = $(this).data('id');

        layer.alert('确定重置该用户密码?',{
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,btn: ['确定','取消'] //按钮
            ,icon: 6    // icon
        },function(){
            $.ajax({
                type:"PUT",
                url:contextPath+"/user/reset/" + userid,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "重置成功!"){
                        layer.msg('重置成功!<br>页面将在1秒后刷新...', {
                            time: 1000 //1s后自动关闭
                        });
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                    }
                },
                error:function (e) {
                }
            });
        });
    });

    $("body").on("click",".reneww",function(){
        var userid = $(this).data('id');

        layer.alert('确定恢复该用户?',{
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,btn: ['确定','取消'] //按钮
            ,icon: 6    // icon
        },function(){
            $.ajax({
                type:"PUT",
                url:contextPath+"/user/renew/" + userid,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "恢复成功!"){
                        layer.msg('恢复成功!<br>页面将在1秒后刷新...', {
                            time: 1000 //1s后自动关闭
                        });
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                    }
                },
                error:function (e) {
                }
            });
        });
    });

    $("body").on("click",".deletee",function(){
        var userid = $(this).data('id');

        layer.alert('确定删除该用户?',{
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,btn: ['确定','取消'] //按钮
            ,icon: 6    // icon
        },function(){
            $.ajax({
                type:"DELETE",
                url:contextPath+"/user/cc/" + userid,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "船厂用户删除成功!"){
                        layer.msg('船厂用户删除成功!<br>页面将在1秒后刷新...');
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                    }
                },
                error:function (e) {
                }
            });
        });
    });

    $("body").on("click",".moress",function(){
        var userid = $(this).data('id');

        $.get('DetailsShipyard.html',function(DetailsShipyard) {
            index = layer.open({
                type: 1,
                title: '详情',
                content: DetailsShipyard,
                area: ['400px', '580px'],
                maxmin: false,
                success:function (layero,index) {
                    $.ajax({
                        type: "GET",
                        url: contextPath + "/user/cc/" + userid,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            $('#name').attr("value",data.name);
                            $('#sex').attr("value",data.sex);
                            $('#state').attr("value",data.state);
                            $('#coname').attr("value",data.coname);
                            $('#phone').attr("value",data.username);
                            $('#userid').attr("value",data.userid);
                            $('#fax').attr("value",data.fax);
                            $('#zipcode').attr("value",data.zipcode);
                            $('#crecode').attr("value",data.crecode);
                            $('#ccadr').attr("value",data.ccadr);
                            $('#writename').attr("value",data.writename);
                            $('#rgsttime').attr("value",data.rgsttime);
                        },
                        error: function (e) {

                        }
                    });
                },
                yes: function(index) {
                    console.log(index);
                },
                full: function(elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function() {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
            form.render();
        });
    });

    $('body').on('click', '#close', function(){
        layer.close(index);
        return false;
    });

    $('body').on('click', '#closex', function(){
        layer.close(index);
        return false;
    });

    $('#search').on('click', function() {
        var type = $('#search_type').val();
        var value = $('#search_value').val();
        if ((value === '' || value.length === 0) && type != "Sex" && type != "State") {
            layer.msg('请输入关键字！');
            return;
        } else if (type == "Sex") {
            var sexx = $('input:radio[name="sex"]:checked').val();
            if (!sexx) {
                layer.msg('请选择性别！');
                return;
            } else {
                value = sexx;
            }
        } else if (type == "State") {
            var statex = $('input:radio[name="state"]:checked').val();
            if (!statex) {
                layer.msg('请选择用户状态！');
                return;
            } else {
                value = statex;
            }
        }
        $('#s_type').attr("value",type);
        $('#s_value').attr("value",value);
        form.render();

        paging.get({
            value: value,
            type: type
        });
    });

    $('#showall').on('click', function() {
        $('#search_type').val("");
        $('#search_value').val("");
        $('#s_type').removeAttr("value");
        $('#s_value').removeAttr("value");
        form.render();
        $('#search_state').hide();
        $('#search_value').show();
        $('#search_sex').hide();
        paging.get({
            value: null,
            type: null
        });
    });

    $('#add').on('click', function() {
        $.get('NewShipyard.html', null, function(NewShipyard) {
            layer.open({
                type: 1,
                title: '新增船厂用户',
                content: NewShipyard,
                area: ['400px', '480px'],
                maxmin: false,
                success:function (layero, index) {
                    layero.find("#all-info").html("*后为必填项");
                    
                    layero.find("#name").on("blur",function () {
                        var name = $(this).val();
                        var nameRegex=/(^[\u4e00-\u9fa5]{2,4}$)|(^[a-zA-Z]{3,7}$)/;
                        if(!nameRegex.test(name)){
                            if(name == ""){
                                layero.find("#nameInfo").css("color","red");
                                layero.find("#nameInfo").attr("class","fa fa-times");
                                layero.find("#all-info").html("*姓名不能为空");
                            }else{
                                layero.find("#nameInfo").css("color","red");
                                layero.find("#nameInfo").attr("class","fa fa-times");
                                layero.find("#all-info").html("*姓名由2-4个中文字或3-7个英文字母组成");
                            }
                        }else{
                            layero.find("#nameInfo").css("color","green");
                            layero.find("#nameInfo").attr("class","fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#coname").on("blur",function () {
                        var coname = $(this).val();
                        var nameRegex=/^[\u4e00-\u9fa5\\(\\)\\（\\）]{2,20}$/;
                        if(!nameRegex.test(coname)){
                            if(coname == ""){
                                layero.find("#conameInfo").css("color","red");
                                layero.find("#conameInfo").attr("class","fa fa-times");
                                layero.find("#all-info").html("*企业名称不能为空");
                            }else{
                                layero.find("#conameInfo").css("color","red");
                                layero.find("#conameInfo").attr("class","fa fa-times");
                                layero.find("#all-info").html("*企业名称由2-20个中文字组成");
                            }
                        }else{
                            layero.find("#conameInfo").css("color","green");
                            layero.find("#conameInfo").attr("class","fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#phone").on("blur",function () {
                        var username = $(this).val();
                        var nameRegex = /^1[345789]\d{9}$/;
                        if (!nameRegex.test(username)) {
                            if (username == "") {
                                layero.find("#usernameInfo").css("color", "red");
                                layero.find("#usernameInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*手机号码不能为空");
                            } else {
                                layero.find("#usernameInfo").css("color", "red");
                                layero.find("#usernameInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*请正确填写手机号码");
                            }
                        } else {
                            layero.find("#usernameInfo").css("color", "green");
                            layero.find("#usernameInfo").attr("class", "fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#userid").on("blur",function () {
                        var userid = $(this).val();
                        var nameRegex = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|30|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|X)$/;
                        if (!nameRegex.test(userid)) {
                            if (userid == "") {
                                layero.find("#useridInfo").css("color", "red");
                                layero.find("#useridInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*身份证号不能为空");
                            } else {
                                layero.find("#useridInfo").css("color", "red");
                                layero.find("#useridInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*请正确填写身份证号");
                            }
                        } else {
                            layero.find("#useridInfo").css("color", "green");
                            layero.find("#useridInfo").attr("class", "fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#fax").on("blur",function () {
                        var fax = $(this).val();
                        var nameRegex=/^.{1,20}$/;
                        if (!nameRegex.test(fax)) {
                            if (fax == "") {
                                layero.find("#conameInfo").css("color","green");
                                layero.find("#conameInfo").attr("class","fa fa-check");
                                layero.find("#all-info").html("");
                            } else {
                                layero.find("#faxInfo").css("color", "red");
                                layero.find("#faxInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*请正确填写传真");
                            }
                        } else {
                            layero.find("#faxInfo").css("color", "green");
                            layero.find("#faxInfo").attr("class", "fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#zipcode").on("blur",function () {
                        var zipcode = $(this).val();
                        var nameRegex=/^.{1,20}$/;
                        if (!nameRegex.test(zipcode)) {
                            if (zipcode == "") {
                                layero.find("#conameInfo").css("color","green");
                                layero.find("#conameInfo").attr("class","fa fa-check");
                                layero.find("#all-info").html("");
                            } else {
                                layero.find("#zipcodeInfo").css("color", "red");
                                layero.find("#zipcodeInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*请正确填写邮编");
                            }
                        } else {
                            layero.find("#zipcodeInfo").css("color", "green");
                            layero.find("#zipcodeInfo").attr("class", "fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#crecode").on("blur",function () {
                        var crecode = $(this).val();
                        var nameRegex=/^[0-9A-Z]{18}$/;
                        if (!nameRegex.test(crecode)) {
                            if (crecode == "") {
                                layero.find("#crecodeInfo").css("color", "red");
                                layero.find("#crecodeInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*社会统一信用代码不能为空");
                            } else {
                                layero.find("#crecodeInfo").css("color", "red");
                                layero.find("#crecodeInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*请正确填写社会统一信用代码");
                            }
                        } else {
                            layero.find("#crecodeInfo").css("color", "green");
                            layero.find("#crecodeInfo").attr("class", "fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#ccadr").on("blur",function () {
                        var ccadr = $(this).val();
                        var nameRegex=/(^[\u4e00-\u9fa5\(\)\（\）A-Za-z0-9]{2,30}$)/;
                        if (!nameRegex.test(ccadr)) {
                            if (ccadr == "") {
                                layero.find("#ccadrInfo").css("color", "red");
                                layero.find("#ccadrInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*地址不能为空");
                            } else {
                                layero.find("#ccadrInfo").css("color", "red");
                                layero.find("#ccadrInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*地址由30位中文字、数字和括号组成");
                            }
                        } else {
                            layero.find("#ccadrInfo").css("color", "green");
                            layero.find("#ccadrInfo").attr("class", "fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });

                    layero.find("#writename").on("blur",function () {
                        var writename = $(this).val();
                        var nameRegex=/(^[\u4e00-\u9fa5]{2,4}$)|(^[a-zA-Z]{3,7}$)/;
                        if (!nameRegex.test(writename)) {
                            if (writename == "") {
                                layero.find("#conameInfo").css("color","green");
                                layero.find("#conameInfo").attr("class","fa fa-check");
                                layero.find("#all-info").html("");
                            } else {
                                layero.find("#writenameInfo").css("color", "red");
                                layero.find("#writenameInfo").attr("class", "fa fa-times");
                                layero.find("#all-info").html("*请正确填写负责人");
                            }
                        } else {
                            layero.find("#writenameInfo").css("color", "green");
                            layero.find("#writenameInfo").attr("class", "fa fa-check");
                            layero.find("#all-info").html("");
                        }
                    });
                },
                yes: function(index) {
                    console.log(index);
                },
                full: function(elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function() {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
            form.render();
        });
    });

    form.on('select(type)', function(data){
        if(data.value == 'Sex'){
            $('#search_state').hide();
            $('#search_value').hide();
            $('#search_sex').show();
        } else if(data.value == 'State'){
            $('#search_state').show();
            $('#search_value').hide();
            $('#search_sex').hide();
        } else if(data.value == 'Name' || data.value == 'phoneNum' || data.value == 'Address'){
            $('#search_state').hide();
            $('#search_value').show();
            $('#search_sex').hide();
        }
    });

    $('.site-table tbody tr').on('click', function(event) {
        var $this = $(this);
        var $input = $this.children('td').eq(0).find('input');
        $input.on('ifChecked', function(e) {
            $this.css('background-color', '#EEEEEE');
        });
        $input.on('ifUnchecked', function(e) {
            $this.removeAttr('style');
        });
        $input.iCheck('toggle');
    }).find('input').each(function() {
        var $this = $(this);
        $this.on('ifChecked', function(e) {
            $this.parents('tr').css('background-color', '#EEEEEE');
        });
        $this.on('ifUnchecked', function(e) {
            $this.parents('tr').removeAttr('style');
        });
    });

    exports('ShipyardM', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});